/*
 * main_con.c
 *
 * Created: 2012-08-24 18:03:29
 *  Author: Antrykot
 */ 

#include "../sc-p/sc-p.h"
#include "../sc-p/utils.h"
#include "../sc-p/trans-p.h"
#include "../sc-p/measure.h"

extern u8 add_params_num ;					// set_add_data

u8 allow_meas = 0;

int main(void)
{
	WAITMS(30);
	setup_io();
	init_eeprom();
	DCDC_ON;
	WAITMS(300);
	
	sei();
	TR_P_ENABLE;
	
	PTX_H;
	
	while(1)
	{
		if (allow_meas)
		{
			calc_l(0);	// normal measure
			allow_meas = 0;
		}

		calc_aku();
		calc_pow();
		calc_dc();

		
		WAITMS(30);

	}
}


void interpret()
{
	switch(r_addr)
	{
		case P_ADDR_DCOFF:
		{
			DCDC_OFF;
			break;
		}
		
		case P_ADDR_INCAKU:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku += ADD_LESS;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_DECAKU:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku -= ADD_LESS;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_INCPOW:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow += ADD_LESS;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		case P_ADDR_DECPOW:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow -= ADD_LESS;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		case P_ADDR_INCAKUM:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku += ADD_MORE;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_DECAKUM:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku -= ADD_MORE;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_INCPOWM:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow += ADD_MORE;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		case P_ADDR_DECPOWM:
		{
			x_pow = read_word(EEP_X_POW); 
			x_pow -= ADD_MORE;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
			
		case P_ADDR_L_FACTORY:
		{

			x_scn = 3300;
			x_szn = 660;

			x_cn = 3300;
			x_zn = 660;
			
			stn = 207;
			write_word(EEP_X_SZN,x_szn);
			write_word(EEP_X_SCN,x_scn);
			
			write_word(EEP_X_ZN,x_zn);
			write_word(EEP_X_CN,x_cn);

			write_word(EEP_STN,stn);
			break;
		}
		
		case P_ADDR_L_RESET:
		{
			ser_def_low();
			break;
		}
		
		
		case P_ADDR_SCALLZ:
		{
			cal_l_zero();
			
			break;
		}
		case P_ADDR_SCALLS:
		{
			cal_l_sens(r_data);
			
			x_szn = x_zn;
			x_scn = x_cn;
			write_word(EEP_X_SZN,x_szn);
			write_word(EEP_X_SCN,x_scn);
			
			break;
		}

		
		case P_ADDR_CALLZ:
		{
			cal_l_zero();
			
			break;
		}
		case P_ADDR_CALLS:
		{
			cal_l_sens(r_data);

			break;
		}

		case P_ADDR_RESET_DEFAULTS:
		{
			x_zn = read_word(EEP_X_ZN);
			x_cn = read_word(EEP_X_CN);
			stn = read_word(EEP_STN);
			break;
		}
	}
	
	
}





void cal_l_zero()
{
	adc_readout = ltc_read(0);

	x_zn = adc_readout;
	
	stn = read_word(EEP_STN);
	x_cn = read_word(EEP_X_SCN);


	write_word(200,x_zn);
}

void cal_l_sens(u16 sense)
{
	stn = sense;
	
	adc_readout = ltc_read(0);
	
	x_cn = adc_readout;

	u16 temp = read_temp();
	
	
	
	write_word(208,x_cn); 
	write_word(216,temp);
	
	float fix;
	if (temp & 0x8000)
	{
		temp &= 0x7FFF;
		temp /= 10;
		fix = 1 + temp * 0.00575;
	}
	else
	{
		temp /= 10;
		if (temp >= 20)
		{
			temp -= 20;
			fix = 1 - temp * 0.003;
		}
		else
		{
			temp = 20 - temp;
			fix = 1 + temp * 0.00575;
		}
		
	}
	x_cn *= fix;
	
	write_word(EEP_X_ZN,x_zn);
	write_word(EEP_X_CN,x_cn);
	write_word(EEP_STN,stn);
	
	write_double(224,fix);
	
}



////////// GLOBAL

u16 eep_cnt = 0;
u16 timer_cnt = 0;
u16 timer_cnt_h = 0;

float m1,m2,m3,m4,m5,m6,m7,m8,m9;
u16 x0,x1,x2;
float w1,w2,w3,w4,w5;
float w_det;

double swxa;
double swxb;
double swxc;

double wxa;
double wxb;
double wxc;

double snxa;
double snxb;

double nxa;
double nxb;

u16 x_szw;
u16 x_scw;

u16 x_zw;
u16 x_cw;

u16 x_szn;
u16 x_scn;

u16 x_zn;
u16 x_cn;

u8 t_addr;
u16 t_data;
u16 t_pom;
u8 t_crc;

u8 r_addr;
u16 r_data;
u8 r_crc;

u8 tr_ok = 0;

u8 crc_data[10];
u8 crc_data_len;

#ifdef MEDIA_PS

float ap_a;
float ap_b;

#endif

#ifdef MEDIA_PD

float ap_a;
float ap_b;

#endif
